package cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount;

import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.module.agent.convert.withdrawaccount.AgentWithdrawAccountConvert;
import cn.iocoder.yudao.module.agent.service.withdrawaccount.AgentWithdrawAccountService;

@Tag(name = "管理后台 - 代理人佣金提现账户")
@RestController
@RequestMapping("/agent/withdraw-account")
@Validated
public class AgentWithdrawAccountController {

    @Resource
    private AgentWithdrawAccountService withdrawAccountService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建代理人佣金提现账户")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:create')")
    public CommonResult<Long> createWithdrawAccount(@Valid @RequestBody AgentWithdrawAccountCreateReqVO createReqVO) {
        return success(withdrawAccountService.createWithdrawAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理人佣金提现账户")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:update')")
    public CommonResult<Boolean> updateWithdrawAccount(@Valid @RequestBody AgentWithdrawAccountUpdateReqVO updateReqVO) {
        withdrawAccountService.updateWithdrawAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理人佣金提现账户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:delete')")
    public CommonResult<Boolean> deleteWithdrawAccount(@RequestParam("id") Long id) {
        withdrawAccountService.deleteWithdrawAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理人佣金提现账户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:query')")
    public CommonResult<AgentWithdrawAccountRespVO> getWithdrawAccount(@RequestParam("id") Long id) {
        AgentWithdrawAccountDO withdrawAccount = withdrawAccountService.getWithdrawAccount(id);
        return success(AgentWithdrawAccountConvert.INSTANCE.convert(withdrawAccount));
    }

    @GetMapping("/list")
    @Operation(summary = "获得代理人佣金提现账户列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:query')")
    public CommonResult<List<AgentWithdrawAccountRespVO>> getWithdrawAccountList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentWithdrawAccountDO> list = withdrawAccountService.getWithdrawAccountList(ids);
        return success(AgentWithdrawAccountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理人佣金提现账户分页")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:query')")
    public CommonResult<PageResult<AgentWithdrawAccountRespVO>> getWithdrawAccountPage(@Valid AgentWithdrawAccountPageReqVO pageVO) {
        PageResult<AgentWithdrawAccountDO> pageResult = withdrawAccountService.getWithdrawAccountPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), AgentWithdrawAccountDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(AgentWithdrawAccountConvert.INSTANCE.convertPage2(pageResult, memberMap));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理人佣金提现账户 Excel")
    @PreAuthorize("@ss.hasPermission('agent:withdraw-account:export')")
    @OperateLog(type = EXPORT)
    public void exportWithdrawAccountExcel(@Valid AgentWithdrawAccountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgentWithdrawAccountDO> list = withdrawAccountService.getWithdrawAccountList(exportReqVO);
        // 导出 Excel
        List<AgentWithdrawAccountExcelVO> datas = AgentWithdrawAccountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代理人佣金提现账户.xls", "数据", AgentWithdrawAccountExcelVO.class, datas);
    }

}
